﻿
namespace RedisClientSample
{
    internal class Program
    {
        static void Main(string[] args)
        {
            RedisHelper.SetConnectString("localhost:6379,allowAdmin=true");
            RedisHelper redis = RedisHelper.Instance;

            // 2. 基本FIFO队列使用
            Console.WriteLine("=== 基本FIFO队列测试 ===");

            // 生产者：按顺序添加消息
            redis.QueuePush("order_queue", "订单1");
            redis.QueuePush("order_queue", "订单2");
            redis.QueuePush("order_queue", "订单3");
            redis.QueuePush("order_queue", "订单4");
            redis.QueuePush("order_queue", "订单5");

            Console.WriteLine($"队列长度: {redis.QueueLength("order_queue")}");

            // 消费者：按FIFO顺序获取消息
            string message1 = redis.QueuePop<string>("order_queue"); // 应该是"订单1"
            string message2 = redis.QueuePop<string>("order_queue"); // 应该是"订单2"
            string message3 = redis.QueuePop<string>("order_queue"); // 应该是"订单3"

            Console.WriteLine($"收到消息1: {message1}");
            Console.WriteLine($"收到消息2: {message2}");
            Console.WriteLine($"收到消息3: {message3}");

            // 3. 使用安全版本（推荐）
            Console.WriteLine("=== 安全FIFO队列测试 ===");

            // 添加更多消息
            redis.QueuePush("safe_queue", "安全消息1");
            redis.QueuePush("safe_queue", "安全消息2");

            // 安全获取
            string safeMsg1 = redis.QueuePopSafe<string>("safe_queue");
            string safeMsg2 = redis.QueuePopSafe<string>("safe_queue");
            string safeMsg3 = redis.QueuePopSafe<string>("safe_queue"); // 队列已空，返回null

            Console.WriteLine($"安全消息1: {safeMsg1}");
            Console.WriteLine($"安全消息2: {safeMsg2}");
            Console.WriteLine($"安全消息3: {(safeMsg3 == null ? "null" : safeMsg3)}");

            // 4. 批量FIFO操作
            Console.WriteLine("=== 批量FIFO操作测试 ===");

            // 批量添加
            for (int i = 1; i <= 10; i++)
            {
                redis.QueuePush("batch_queue", $"批量消息{i}");
            }

            // 批量获取（保持FIFO顺序）
            List<string> batchMessages = redis.QueuePopMultiple<string>("batch_queue", 5);
            Console.WriteLine($"批量获取到 {batchMessages.Count} 条消息:");
            foreach (var msg in batchMessages)
            {
                Console.WriteLine($"  - {msg}");
            }

            // 5. 查看队列内容（不移除）
            Console.WriteLine("=== 查看队列内容 ===");
            redis.QueuePush("peek_queue", "查看消息1");
            redis.QueuePush("peek_queue", "查看消息2");
            redis.QueuePush("peek_queue", "查看消息3");

            // 查看但不移除
            string peekMsg = redis.QueuePeek<string>("peek_queue");
            Console.WriteLine($"队列头部消息: {peekMsg}");

            // 查看多个消息
            List<string> peekMessages = redis.QueueRange<string>("peek_queue", 0, 2);
            Console.WriteLine("前3条消息:");
            foreach (var msg in peekMessages)
            {
                Console.WriteLine($"  - {msg}");
            }

            // 队列长度应保持不变
            Console.WriteLine($"查看后队列长度: {redis.QueueLength("peek_queue")}");

            // 6. 复杂对象FIFO队列


            Console.WriteLine("=== 复杂对象FIFO测试 ===");

            // 添加复杂对象
            var order1 = new Order { OrderId = "ORD001", Amount = 100.50m, CreatedAt = DateTime.Now };
            var order2 = new Order { OrderId = "ORD002", Amount = 200.75m, CreatedAt = DateTime.Now.AddMinutes(1) };

            redis.QueuePush("object_queue", order1);
            redis.QueuePush("object_queue", order2);

            // 按FIFO顺序获取复杂对象
            Order receivedOrder1 = redis.QueuePop<Order>("object_queue");
            Order receivedOrder2 = redis.QueuePop<Order>("object_queue");

            Console.WriteLine($"第一个订单: {receivedOrder1?.OrderId}, 金额: {receivedOrder1?.Amount}");
            Console.WriteLine($"第二个订单: {receivedOrder2?.OrderId}, 金额: {receivedOrder2?.Amount}");

            Console.WriteLine("=== 发布订阅测试 ===");

            CSRedisCoreHelper.SetConnectString("localhost:6379,allowAdmin=true");
            CSRedisCoreHelper.Instance.Subscribe("news", (channel, message) =>
            {
                Console.WriteLine($"收到消息 - 频道: {channel}, 内容: {message}");
            }, onSubscribe: (channel) =>
            {
                Console.WriteLine($"成功订阅频道: {channel}");
            });

            //// 在另一个线程中订阅
            //redis.Subscribe("news", (channel, message) =>
            //{
            //    Console.WriteLine($"收到消息 - 频道: {channel}, 内容: {message}");
            //},onSubscribe: (channel) =>
            //{
            //    Console.WriteLine($"成功订阅频道: {channel}");
            //});
            // 等待订阅建立
            Thread.Sleep(1000);

            #region 一键通知

            // 订阅所有键的过期事件
            //redis.SubscribeKeyExpiration(key =>
            //{
            //    Console.WriteLine($"键 {key} 已过期！");
            //});

            CSRedisCoreHelper.Instance.SubscribeKeyExpiration(key =>
            {
                Console.WriteLine($"键 {key} 已过期！");
            });

            // 设置键并自动订阅过期
            redis.SetWithExpiryNotification(
                "session:123",
                "data",
                TimeSpan.FromSeconds(10),
                key => Console.WriteLine($"{key} 会话已过期")
            );

            #endregion

            // 发布消息
            redis.Publish("news", "这是一条新闻消息");
            redis.Publish("news", "这是另一条新闻消息");

            // 等待消息处理
            Thread.Sleep(2000);

            Console.ReadKey();
        }
    }

    public class Order
    {
        public string OrderId { get; set; }
        public decimal Amount { get; set; }
        public DateTime CreatedAt { get; set; }
    }
}
